package com.panda.web.service.impl;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.*;

public class WeChatPayExample {


    private static final String APP_ID = "wx83de228493c38c1c";
    private static final String MCH_ID = "1623988041";
    private static final String API_KEY = "Shandongweichangwangluo123456789";
    private static final String NOTIFY_URL = "https://houtaiguanli.yanbeibao2021.com/web/order/notify";
    private static final String TRADE_TYPE = "JSAPI";

    public static void main(String[] args) throws Exception {

    }

    private static String generateNonceStr() {
        return UUID.randomUUID().toString().replace("-", "").substring(0, 32);
    }

    private static String generateSignature(Map<String, String> params, String apiKey) throws Exception {
        List<String> keys = new ArrayList<>(params.keySet());
        Collections.sort(keys);

        StringBuilder sb = new StringBuilder();
        for (String key : keys) {
            sb.append(key).append("=").append(params.get(key)).append("&");
        }
        sb.append("key=").append(apiKey);

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest(sb.toString().getBytes("UTF-8"));
        return byteArrayToHexString(digest).toUpperCase();
    }

    private static String byteArrayToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    private static String mapToXml(Map<String, String> params) {
        StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            sb.append("<").append(entry.getKey()).append(">");
            sb.append(entry.getValue());
            sb.append("</").append(entry.getKey()).append(">");
        }
        sb.append("</xml>");
        return sb.toString();
    }

    private static String sendPostRequest(String url, String xmlData) throws Exception {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(xmlData, "UTF-8"));
            httpPost.setHeader("Content-Type", "application/xml");

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        }
    }

    private static String generateOrderNo(String openId) {
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date()); // 当前日期
        String randomStr = String.format("%04d", new Random().nextInt(10000)); // 4位随机数
        return date + 1 + randomStr; // 订单号 = 日期 + 用户ID + 随机数
    }

}